package com.atguigu.tingshu.search.api;

import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.result.ResultCodeEnum;
import com.atguigu.tingshu.query.search.AlbumIndexQuery;
import com.atguigu.tingshu.search.service.SearchService;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexSearchVo;
import com.atguigu.tingshu.vo.search.AlbumInfoIndexVo;
import com.atguigu.tingshu.vo.search.AlbumSearchResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Tag(name = "搜索专辑管理")
@RestController
@RequestMapping("api/search/albumInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class SearchApiController {

    @Autowired
    private SearchService searchService;

    @Operation(summary = "上架专辑")
    @GetMapping("/upperAlbum/{albumId}")
    public Result upperAlbum(@PathVariable(value = "albumId") Long albumId){
        searchService.upperAlbum(albumId);         //  调用服务层方法
        return Result.ok();
    }

    @Operation(summary = "下架专辑")
    @GetMapping("/lowerAlbum/{albumId}")
    public Result lowerAlbum(@PathVariable(value = "albumId") Long albumId){
        searchService.lowerAlbum(albumId);         //  调用服务层方法
        return Result.ok();
    }

    @Operation(summary = "批量上架")
    @GetMapping("/batchUpperAblum")
    public Result upperAlbum(){
        for (long i = 1; i <= 1500; i++) {   // 专辑id为1数据补全，缺少专辑属性数据
            searchService.upperAlbum(i);
        }
        return Result.ok();
    }

    @Operation(summary = "专辑列表搜素")
    @PostMapping
    public Result<AlbumSearchResponseVo> search(@RequestBody AlbumIndexQuery albumIndexQuery) throws IOException {
        AlbumSearchResponseVo albumSearchResponseVo = searchService.search(albumIndexQuery);
        return Result.ok(albumSearchResponseVo);
    }
    @Operation(summary = "获取频道页数据")
    @GetMapping("/channel/{category1Id}")
    public Result<List<AlbumInfoIndexSearchVo>> channel(@PathVariable Long category1Id) {
        List<AlbumInfoIndexSearchVo> albumInfoIndexSearchVoList = searchService.channel(category1Id) ;
        return Result.ok(albumInfoIndexSearchVoList) ;
    }


    @Operation(summary = "关键字自动补全")
    @GetMapping("completeSuggest/{keyword}")
    public Result completeSuggest(@PathVariable String keyword) {
        List<String> list = searchService.completeSuggest(keyword);
        return Result.ok(list);
    }
    @Operation(summary = "更新最近专辑排行")
    @GetMapping("updateLatelyAlbumRanking")
    public Result updateLatelyAlbumRanking() {
        searchService.updateLatelyAlbumRanking();
        return Result.ok();
    }

    @GetMapping(value = "/findRankingList/{category1Id}/{dimension}")
    public Result<List<AlbumInfoIndexVo>> findRankingList(@PathVariable(value = "category1Id") Long category1Id, @PathVariable(value = "dimension")String dimension) {
        List<AlbumInfoIndexVo> list = searchService.findRankingList(category1Id , dimension) ;
        return Result.build(list , ResultCodeEnum.SUCCESS) ;
    }

}

